<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['typeofcall'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['typeofcall'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
	$partyArr = array();
	$partyCnt = 0;
	$selectParty = "SELECT * FROM party ORDER BY companyName";
	$selectPartyRes = mysql_query($selectParty);
	while($partyRow = mysql_fetch_array($selectPartyRes))
	{
		$partyArr['partyId'][$partyCnt]     = $partyRow['partyId'];
		$partyArr['companyName'][$partyCnt] = $partyRow['companyName'];
		$partyCnt++;
	}
	
	if(isset($_POST['insertBtn']))
	{
	  $selectAchievedTarget = "SELECT userTargetId,achievedTarget
	                             FROM usertarget
	                            WHERE userId =".$_SESSION['s_activeUserId'.$cfgPack]." 
	                              AND month ='".date('Y-m-01')."'";
	  $selectAchievedTargetRes = mysql_query($selectAchievedTarget);
	  $achievedTargetRow = mysql_fetch_array($selectAchievedTargetRes);
	  
	  $insertAchievedTarget = "INSERT INTO usertargetachieved(userTargetId,partyId,orderValue,orderDate)
	                            VALUE ('".$achievedTargetRow['userTargetId']."',".$_POST['partyId'].",'".$_POST['orderValue']."','".date('Y-m-d')."')";
    $insertAchievedTargetRes = mysql_query($insertAchievedTarget);	                            
	  
	  $updateUserTarget = "UPDATE usertarget
	                          SET achievedTarget = ".$achievedTargetRow['achievedTarget']." + ".$_POST['orderValue']."
	                        WHERE userId =".$_SESSION['s_activeUserId'.$cfgPack]." 
	                          AND month ='".date('Y-m-01')."'";
	  $updateUserTargetRes = mysql_query($updateUserTarget);                          
	  if(!$updateUserTargetRes)
	    die("Query Not Updated : ".mysql_error());
	  else
	    header("Location:dailyReport.php");
  }
  include("./bottom.php");
  $smarty->assign("partyArr",$partyArr);
  $smarty->display("userTargetAchieved.tpl");
}
?>